package com.juno.cafenoa.sqlite;

import java.util.ArrayList;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.util.Log;

import com.juno.cafenoa.menu.CategoryBasicItem;
import com.juno.cafenoa.menu.MenuBasicItem;

public class CafeNoaDBAdapter {
	private SQLiteDatabase db;

	public void open(Context context) {
		try {
			db = (new CafeNoaDBDataHelper(context)).getWritableDatabase();
		} catch (SQLiteException e) {
			db = (new CafeNoaDBDataHelper(context)).getReadableDatabase();
		}
	}

	public void insertMenuData(int productId, int productCategory,
			String productName, int productPrice, String productImage,
			String productContent, String productOption) {
		String query = null;
		query = "INSERT INTO " + CafeNoaDBConstants.PRODUCT_TABLE + "("
				+ CafeNoaDBConstants.PRODUCT_ID + ","
				+ CafeNoaDBConstants.PRODUCT_CATEGORY + ","
				+ CafeNoaDBConstants.PRODUCT_NAME + ","
				+ CafeNoaDBConstants.PRODUCT_PRICE + ","
				+ CafeNoaDBConstants.PRODUCT_IMAGE + ","
				+ CafeNoaDBConstants.PRODUCT_CONTENT + ","
				+ CafeNoaDBConstants.PRODUCT_OPTION + ") VALUES ('"
				+ productId + "', '" + productCategory + "', '" + productName
				+ "', '" + productPrice + "', '" + productImage + "', '"
				+ productContent+"', '" + productOption + "')";
		Log.v("CafeNoa", "DB Query : " + query);
		db.execSQL(query);
	}

	public void insertCategoryData(int categoryId, String categoryName) {
		String query = null;
		query = "INSERT INTO " + CafeNoaDBConstants.CATEGORY_TABLE + "("
				+ CafeNoaDBConstants.CATEGORY_ID + ","
				+ CafeNoaDBConstants.CATEGORY_NAME + ") VALUES ('" + categoryId
				+ "', '" + categoryName + "')";
		db.execSQL(query);
	}

	public ArrayList<MenuBasicItem> selectMenuData(int categoryNumber) {
		ArrayList<MenuBasicItem> list = new ArrayList<MenuBasicItem>();
		String query = null;
		query = "SELECT " + CafeNoaDBConstants.PRODUCT_ID + ", "
				+ CafeNoaDBConstants.PRODUCT_CATEGORY + ", "
				+ CafeNoaDBConstants.PRODUCT_NAME + ", "
				+ CafeNoaDBConstants.PRODUCT_PRICE + ", "
				+ CafeNoaDBConstants.PRODUCT_IMAGE + ", "
				+ CafeNoaDBConstants.PRODUCT_CONTENT + ", "
				+ CafeNoaDBConstants.PRODUCT_OPTION + " FROM "
				+ CafeNoaDBConstants.PRODUCT_TABLE + " WHERE "
				+ CafeNoaDBConstants.PRODUCT_CATEGORY + "=" + categoryNumber;
		Cursor c = db.rawQuery(query, null);
		if (c.moveToFirst() == true) {
			list.add(new MenuBasicItem(Integer.valueOf(c.getString(0)), Integer
					.valueOf(c.getString(1)), c.getString(2), Integer.valueOf(c
					.getString(3)), c.getString(4), c.getString(5), c.getString(6)));
			while (c.moveToNext()) {
				Log.d("CafeNoaDBAdapter.java", "c.ImgUrl() = " + c.getString(4));
				list.add(new MenuBasicItem(Integer.valueOf(c.getString(0)),
						Integer.valueOf(c.getString(1)), c.getString(2),
						Integer.valueOf(c.getString(3)), c.getString(4), c
								.getString(5), c.getString(6)));
			}
		}
		return list;
	}

	public ArrayList<CategoryBasicItem> selectCategoryData() {
		ArrayList<CategoryBasicItem> list = new ArrayList<CategoryBasicItem>();
		String query = null;
		query = "SELECT " + CafeNoaDBConstants.CATEGORY_ID + ", "
				+ CafeNoaDBConstants.CATEGORY_NAME + " FROM "
				+ CafeNoaDBConstants.CATEGORY_TABLE;

		Cursor c = db.rawQuery(query, null);
		if (c.moveToFirst() == true) {
			list.add(new CategoryBasicItem(Integer.valueOf(c.getString(0)), c
					.getString(1)));
			while (c.moveToNext()) {
				list.add(new CategoryBasicItem(Integer.valueOf(c.getString(0)),
						c.getString(1)));
			}
		}
		return list;
	}
	
	public void removeMenuData(){
		String query = "DELETE FROM " + CafeNoaDBConstants.CATEGORY_TABLE +" WHERE " + CafeNoaDBConstants.CATEGORY_ID+">0";
		db.execSQL(query);
		String query2 = "DELETE FROM " + CafeNoaDBConstants.PRODUCT_TABLE +" WHERE " + CafeNoaDBConstants.PRODUCT_ID+">0";
		db.execSQL(query2);
		Log.v("delete","table delete success");
		
	}

	// SQLiteDatabase 닫기.
	public void Close() {
		db.close();
	}
}
